<?php

namespace App\Model;


/**
 * Class Cart
 * @package App\Model
 * @User wuqin
 * @Date 2018/3/29 0029
 * @Time 20:38
 */
/**
 * Class Cart
 * @package App\Model
 * @User wuqin
 * @Date 2018/3/31 0031
 * @Time 15:23
 */
/**
 * Class Cart
 * @package App\Model
 * @User wuqin
 * @Date 2018/3/31 0031
 * @Time 15:24
 */
class Cart extends Common
{
    /**
     * @var bool
     */
    public $timestamps = false;
    /**
     * @var string
     */
    protected $primaryKey = 'cart_id';
    /**
     * @var string
     */
    protected $table = 'cart';


    /**
     * 方法 : add_cart
     * @User wuqin
     * @DateTime 2018-03-31 15:24
     * @param $var
     * @return Cart|bool
     */
    public function add_cart($var)
    {
        $this->user_id = $var['user_id'];
        $this->commodity_id = $var['commodity_id'];
        $this->info_id = $var['info_id'];
        $this->color_id = $var['color_id'];
        $this->size_id = $var['size_id'];
        $this->number = $var['number'];
        $this->unit_price = $var['unit_price'];
        $this->all_price = $var['all_price'];

        return $this->save() ? $this : false;
    }

    /**
     * 方法 : update_cart
     * @User wuqin
     * @DateTime 2018-03-31 15:24
     * @param $var
     * @return Cart|bool
     */
    public function update_cart($var)
    {

        $this->number = $var['number'];
        $this->all_price = $var['all_price'];

        return $this->save() ? $this : false;
    }

    public function delete_cart_by_ids($ids)
    {
        if (empty($ids) || !is_array($ids))
            return false;

        $where = array(
            array("whereIn", "cart_id", $ids)
        );

        return $this->deleteByWhere($where);
    }
    /**
     * 方法 : get_cart_by_user_id
     * @User wuqin
     * @DateTime 2018-03-31 15:23
     * @param $user_id
     * @return bool
     */
    public function get_cart_by_user_id($user_id)
    {
        if (empty($user_id))
            return false;

        $where = array(
            array("where", "user_id", "=", $user_id)
        );

        $res = $this->get($where, "*", 0, 0, ["commodity_id", "asc"]);
        if (empty($res))
            return false;
        else
            return $res;
    }

    public function get_cart_by_cart_id($cart_id)
    {
        if (empty($cart_id))
            return false;

        $where = array(
            array("where", "cart_id", "=", $cart_id)
        );

        $res = $this->get($where, "*", 0, 0, ["commodity_id", "asc"]);
        if (empty($res))
            return false;
        else
            return $res;
    }

    public function get_cart_by_cart_ids($cart_id)
    {
        if (empty($cart_id))
            return false;

        $where = array(
            array("whereIn", "cart_id", $cart_id)
        );

        $res = $this->get($where, "*", 0, 0, ["commodity_id", "asc"]);
        if (empty($res))
            return false;
        else
            return $res;
    }
}
